package com.retch.wenchengmao.recodeauthutils.listview;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.retch.wenchengmao.recodeauthutils.R;
import com.retch.wenchengmao.recodeauthutils.listview.entity.Info;

import java.util.ArrayList;
import java.util.List;

/**
 * author wenchengmao
 * time   2017/4/30
 * email  wenchengmao@yeah.net
 */

public class MyAdapter extends BaseAdapter implements Filterable {
    List<Info> data; //这个数据是会改变的，所以要有个变量来备份一下原始数据
    List<Info> backData;//用来备份原始数据
    MyFilter mFilter ;
    public LayoutInflater layoutInflater;
    public Context context;

    public MyAdapter(Context context,List<Info> list)
    {
        this.context=context;
        this.data=list;
        this.backData=list;
        layoutInflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Info getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_info, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Info info=getItem(position);
        holder.name.setText(info.getName());
        holder.money.setText(String.valueOf(info.getMoney()));
        holder.job.setText(info.getJob());

        return convertView;
    }

    //当ListView调用setTextFilter()方法的时候，便会调用该方法
    @Override
    public Filter getFilter() {
        if (mFilter ==null){
            mFilter = new MyFilter();
        }
        return mFilter;
    }
    //我们需要定义一个过滤器的类来定义过滤规则
    class MyFilter extends Filter{
        //我们在performFiltering(CharSequence charSequence)这个方法中定义过滤规则
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults result = new FilterResults();
            List<Info> list ;
            if (TextUtils.isEmpty(charSequence)){//当过滤的关键字为空的时候，我们则显示所有的数据
                list  = backData;
            }else {//否则把符合条件的数据对象添加到集合中
                list = new ArrayList<>();
                for (Info recommend:backData){
                    if (recommend.getName().contains(charSequence)||String.valueOf(recommend.getMoney()).contains(charSequence)
                            ||recommend.getJob().contains(charSequence)){
                        Log.d("recommend",recommend.toString());
                        list.add(recommend);
                    }

                }
            }
            result.values = list; //将得到的集合保存到FilterResults的value变量中
            result.count = list.size();//将集合的大小保存到FilterResults的count变量中

            return result;
        }
        //在publishResults方法中告诉适配器更新界面
        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            data = (List<Info>)filterResults.values;
            if (filterResults.count>0){
                notifyDataSetChanged();//通知数据发生了改变
                Log.d("notifyData","notifyDetChanged");
            }else {
                notifyDataSetInvalidated();//通知数据失效
                Log.d("notifyData","notifyDtChanged");
            }
        }
    }

    class ViewHolder {
        TextView name;
        TextView money;
        TextView job;
        public ViewHolder(View convertView) {
            name = (TextView) convertView.findViewById(R.id.name);
            money = (TextView) convertView.findViewById(R.id.money);
            job=(TextView)convertView.findViewById(R.id.job);
        }
  }
}


